/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * <p>Project: jdbc202202 - ResultSetDemo
 * <p>Powered by webrx On 2022-08-22 10:47:08
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ResultSetDemo {
    Connection conn;


    @Test
    @DisplayName("ResultSet 测试")
    void rsdemo2() {
        try {

            ResultSet rs2 = conn.prepareStatement("select 1 n1,2 n2,3 n3").executeQuery();
            rs2.next();
            System.out.println(rs2.getInt(1));
            System.out.println(rs2.getInt(2));
            System.out.println(rs2.getString("n3"));


            //PreparedStatement 建立rs
            String sql = "select * from t_book";
            PreparedStatement pst = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = pst.executeQuery();
            System.out.println(rs.isBeforeFirst());//true
            System.out.println(rs.isFirst());
            System.out.println(rs.isLast());
            System.out.println(rs.isAfterLast());

            rs.last(); //游标移动到最后一条记录上
            rs.first();
            rs.absolute(3);
            rs.next();
            rs.previous();
            rs.previous();
            rs.previous();
            rs.last();
            System.out.println(rs.isLast());
            System.out.println(rs.isAfterLast());

            //System.out.println(rs.next());
            System.out.println(rs.getString("name"));


            rs.next();
            System.out.println(rs.isAfterLast());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    @DisplayName("ResultSet 测试")
    void rsdemo() {
        try {
            //Statment 建立rs
            ResultSet rs = conn.createStatement().executeQuery("select * from t_book");
            System.out.println(rs.isBeforeFirst());//true
            System.out.println(rs.next());//true

            //PreparedStatement 建立rs
            ResultSet rs2 = conn.prepareStatement("select * from t_book").executeQuery();
            System.out.println(rs2.isBeforeFirst());//true

            //System.out.println(rs2.next());//true
            //System.out.println(rs2.getString("name"));
            //rs2.next();
            //System.out.println(rs2.getString("name"));

            //rs.next();
            //rs.previous()
            //rs.first();
            //rs.last();

            rs.first();
            System.out.println(rs2.getString("name"));

            //if(rs2.isBeforeFirst()){
            //    while(rs2.next()){
            //        System.out.println(rs2.getString("name"));
            //    }
            //}


            ResultSet rs3 = conn.prepareStatement("select * from t_book where 0").executeQuery();
            System.out.println(rs3.isBeforeFirst());//false 没有查询结果，就没有游标对象，
            System.out.println(rs3.next());//false


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @BeforeEach
    void init() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:/db", "root", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterEach
    void close() {
        try {
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
